<template>
<view class="flx_cenetr_content_container">
    <view class="flx_cenetr_content_title">服务商二维码</view>

    <view class="flx_qrode_container">
      <text>长按保存图片</text>
      <view class="flx_qrode_container_image_container">
        <image :data-imageUrl="FxImage" @tap="lookBigImage" @longpress="saveImageToLocal" mode="widthFix" :src="FxImage"></image>
      </view>
      
    </view>

    <!-- <view class='flx_qrode_container'>
      <view>推广链接</view>
      <view>www.ssssssssssssssssss.com</view>
    </view> -->
</view>
</template>

<script>

export default {
  data() {
    return {
      FxImage: ""
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.get_qrcodeFun();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  methods: {
    lookBigImage: function (e) {
      let imgSrc = e.currentTarget.dataset.imageurl;
      console.log(imgSrc);
      let PostImageSrc = imgSrc.replace(/http/, "https"); // let PostImageSrc = imgSrc

      console.log(PostImageSrc);

      if (!imgSrc) {
        return;
      }

      let urls = [];
      urls.push(imgSrc);
      wx.previewImage({
        current: imgSrc,
        // 当前显示图片的http链接
        urls: urls // 需要预览的图片http链接列表

      });
    },
    saveImageToLocal: function (e) {
      let imgSrc = e.currentTarget.dataset.imageurl;
      console.log(imgSrc);
      let PostImageSrc = imgSrc.replace(/http/, "https"); // let PostImageSrc = imgSrc

      console.log(PostImageSrc);

      if (!imgSrc) {
        return;
      }

      let urls = [];
      urls.push(imgSrc);
      wx.previewImage({
        current: imgSrc,
        // 当前显示图片的http链接
        urls: urls // 需要预览的图片http链接列表

      });
      /* wx.downloadFile({
        url: PostImageSrc,
        success:function (res) {
          console.log(res);
          //图片保存到本地
          wx.saveImageToPhotosAlbum({
            filePath: res.tempFilePath,
            success: function (data) {
              console.log(data);
              wx.showToast({
                title: '保存成功',
              })
            },
            fail:function (err) {
              console.log(err);
              if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                console.log("用户一开始拒绝了，我们想再次发起授权")
                console.log('打开设置窗口')
                wx.openSetting({
                  success(settingdata) {
                    console.log(settingdata)
                    if (settingdata.authSetting['scope.writePhotosAlbum']) {
                      console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                      wx.showToast({
                        title: '获取权限成功,请再次保存图片',
                      })
                    }
                    else {
                      console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                      wx.showToast({
                        title: '获取权限失败',
                        image: '/wxcomponents/images/icons/tip.png',
                      })
                    }
                  }
                })
              }
            }
          })
        },
        fail:function(res){
          console.log('url就错了')
          console.log(res)
        }
      }) */
    },
    get_qrcodeFun: function () {
      console.log('-------获取推广二维码信息--------');
      var customIndex = getApp().globalData.AddClientUrl("/get_qrcode.html");
      var that = this; // wx.showLoading({
      //   title: 'loading'
      // })

      getApp().globalData.showToastLoading('loading', true);
      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          if (res.data.errcode == '0') {
            that.setData({
              FxImage: res.data.relateObj
            });
          } else {
            wx.showToast({
              title: res.data.errMsg,
              icon: '/wxcomponents/images/icons/tip.png',
              duration: 1500
            });
          }

          console.log(res.data);
          wx.hideLoading();
        },
        fail: function (res) {
          wx.hideLoading();
          getApp().globalData.loadFail();
        }
      });
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>